<?php
/**
 * Description:
 *
 * @author      Eden
 * @datetime    2017/3/28 20:45
 * @copyright   WuHan HuQi Technology Co.,Ltd.
 * @url         http://cnhuqi.com
 */

class Ticket extends WX_Controller
{
    const  PAGE_SIZE = 20;

    /**
     * @throws Exception
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 获取列表
     */
    public function getTicketList()
    {
        $send['state'] = false;
        do {

            $page = $this->input->get('page');
            $type = $this->input->get("type");

            if (empty($type)) {
                $send['error'] = '参数错误';
                break;
            }

            $page = intval($page) <= 0 ? 1 : intval($page);
            $offset = ($page - 1) * self::PAGE_SIZE;
            $this->load->model('wx/Ticket_Model', 'ticket');

            $res = $this->ticket->getTicketsList($this->userid, $type, $offset, self::PAGE_SIZE);
            if (!empty($res)) foreach ($res as &$item) {
                $item['description'] = nl2br($item['description']);
            }
            $send['state'] = true;
            $send['data'] = !empty($res) ? $res : [];

        } while(false);
        $this->output->send($send);
    }

    /**
     * 数据概览
     */
    public function getSummary() {
        $send['state'] = true;
        $this->load->model('wx/Ticket_Model', 'ticket');
        $summary = $this->ticket->getSummary($this->userid);
        $send['data'] = $summary;
        $this->output->send($send);
    }

    /**
     * 获取 Ticket
     */
    public function getTicket()
    {
        $send['state'] = false;
        do {

            $id = $this->input->post('id');
            if (empty($id)) {
                $send['error'] = '参数错误';
                break;
            }

            // 计算

            $this->load->model('wx/Ticket_Model', 'ticket');
            if ($this->ticket->getTicket($this->userid, $id)) {
                $send['state'] = true;
            }

        } while(false);
        $this->output->send($send);
    }
}
